clear all
set more off

use "$inputs/prowess/Productwise Energy Consumption.dta", clear
gen product_name5 = upper(product4)
* create year variable
tostring(prodener_date3), g(year)
replace year = substr(year, 1, 4)
destring year, replace

keep prodener_cocode product_name5 product_name_mst year
duplicates drop
gen idm = _n

reclink2 product_name5 using "$inputs/handcoded/droplist.dta", idm(idm) idu(idu) gen(score) manytoone
drop if score >= 0.8 & !missing(score)
drop idm idu Uproduct_name5 _merge

collapse (first) product_name_mst, by(prodener_cocode year product_name5)
egen id = group(prodener_cocode product_name5)
tsset id year
tsfill, full
bys id (product_name5): replace product_name5 = product_name5[_N]
bys id (prodener_cocode): replace prodener_cocode = prodener_cocode[1]

gen idu = _n
rename prodener_cocode co_code
rename product_name_mst Uproduct_name_mst
save "$working/energy_temp", replace

use "$working/ppt_clean.dta", clear
replace product_name5 = upper(product_name5)
tostring(prod_date3), g(year)
replace year = substr(year, 1, 4)
destring year, replace
keep products_cocode product_name5 product_name_mst year
duplicates drop
collapse (first) product_name_mst, by(products_cocode year product_name5)
egen id = group(products_cocode product_name5)
tsset id year
tsfill, full
bys id (product_name5): replace product_name5 = product_name5[_N]
bys id (products_cocode): replace products_cocode = products_cocode[1]
rename products_cocode co_code

gen idm = _n

reclink2 co_code product_name5 year using "$working/energy_temp", idm(idm) idu(idu) gen(score) required(co_code year) manytoone 
keep if _merge == 3
format product_name5 %30s
format Uproduct_name5 %30s
preserve

drop if Uyear == .
duplicates drop co_code product_name5 Uproduct_name5, force
save "$working/reclink_short.dta", replace
use "$working/reclink_short.dta", clear
drop if score < .7
keep co_code product_name5 Uproduct_name5
rename Uproduct_name5 product4
save "$working/reclink_short_.dta", replace

